import axios from 'axios';

export default (await import('vue')).defineComponent({
name: 'CoursePlatform',
components: {
CourseTimeSelector,
CreateCourseDialog,
PersonalCenter
},
data() {
return {
showCreateDialog: false,
showCreateForm: false,
newCourse: {
id: '',
semester: '',
name: '',
description: '',
syllabus: '',
time: '{}'
},
activeIndex: '1',
userInfo: {
id: '',
name: '',
role: '',
email: ''
},
courses: [],
coursestatus: [],
unreadMessages: 0,

unreadCourseNotices: 46,
scheduleData: [],
weekDays: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
timePeriods: [
{ time: '【08:00-09:50】', index: 1 },
{ time: '【10:10-12:00】', index: 2 },
{ time: '【14:10-16:00】', index: 3 },
{ time: '【16:20-18:00】', index: 4 },
{ time: '【19:00-20:50】', index: 5 },
{ time: '【21:00-22:00】', index: 6 }
]
};
},
computed: {
currentSemester() {
return this.getCurrentSemester();
}
},
methods: {
handleNavClick(index) {
this.activeIndex = index;

switch (index) {
case '1':
this.fetchCourses();
break;
case '2':
// 这里我们仍然使用 fetchCourses，除非您有特殊需求
this.fetchCourses();
this.showCreateForm = false; // 重置创建表单的显示状态
break;
case '3': //加这个
this.fetchCourseStatus();
break;
}
},
viewCourseDetail(courseId) {
this.$router.push(`/course/${courseId}`);
},
handlePersonalCenter() {
this.activeIndex = '4';
},
getCurrentSemester() {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1; // JavaScript 月份从 0 开始

if (month >= 2 && month <= 7) {
return `${year}-1000`; // 第一学期
} else {
return `${year}-2000`; // 第二学期
}
},
async fetchUserInfo() {
try {
const response = await axios.get('/backend/api/account/profile/', { withCredentials: true });
this.userInfo = {
id: response.data.id,
name: response.data.username,
role: response.data.role,
email: response.data.email,
};
this.isTeacher = response.data.role === 'teacher';
} catch (error) {
console.error('获取用户信息失败:', error);
this.error = '获取用户信息失败';
}
},
async fetchCourses() {
const response = await axios.post('/backend/api/course/courses/', { semester: this.currentSemester },
{ withCredentials: true });
this.courses = response.data;
this.generateScheduleData();
},
generateScheduleData() {
// 初始化课程表数据
this.scheduleData = this.timePeriods.map(period => {
const row = { time: period.time };
this.weekDays.forEach(day => {
row[day] = [];
});
return row;
});

// 填充课程数据
this.courses.forEach(course => {
Object.entries(course.time).forEach(([day, periods]) => {
periods.forEach(period => {
const dayIndex = parseInt(day) - 1;
const rowIndex = this.timePeriods.findIndex(p => p.index === period);
if (rowIndex >= 0 && rowIndex < this.scheduleData.length) {
this.scheduleData[rowIndex][this.weekDays[dayIndex]].push(course.name);
}
});
});
});
},
getRowClass({ row, rowIndex }) {
return 'schedule-row';
},

getCellClass({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 0) return 'time-column';
return 'course-cell';
},

renderCell(h, { row, column, $index }) {
if (column.property === 'time') {
return h('span', {}, row.time);
}
const courses = row[column.property];
if (courses.length === 0) return h('span', {}, '');
return h('div', {}, courses.map(course => {
return h('div', { class: 'course-item' }, course);
}));
},
handleCourseCreated(course) {
// 刷新课程列表
this.fetchCourses();
},
//查看课程状态
// 加这个
async fetchCourseStatus() {
try {
const response = await axios.get('/backend/api/course/student/courses/', { withCredentials: true });
this.coursestatus = response.data;
} catch (error) {
console.error('获取课程状态失败:', error);
}
}
},
mounted() {
this.fetchUserInfo();
this.fetchCourses().then(() => {
this.generateScheduleData();
this.fetchCourseStatus();
});
}
});
